using System;
using System.Collections.Generic;
using System.Text;

namespace CodePlex.Resourceful.Amazon.S3
{
    public class LogRecord
    {
        private string _bucketOwner;
        private string _bucket;
        private string _time;
        private string _remoteIP;
        private string _requestor;
        private string _requestID;
        private string _operation;
        private string _key;
        private string _requestURI;
        private string _httpStatus;
        private string _errorCode;
        private string _bytesSent;
        private string _objectSize;
        private string _totalTime;
        private string _turnAroundTime;
        private string _referrer;
        private string _userAgent;

        public string BucketOwner { get { return _bucketOwner; } }
        public string Bucket { get { return _bucket; } }
        public string Time { get { return _time; } } // [
        public string RemoteIP { get { return _remoteIP; } }
        public string Requestor { get { return _requestor; } }
        public string RequestID { get { return _requestID; } }
        public string Operation { get { return _operation; } }
        public string Key { get { return _key; } }
        public string RequestURI { get { return _requestURI; } }  //
        public string HTTPStatus { get { return _httpStatus; } }
        public string ErrorCode { get { return _errorCode; } }
        public string BytesSent { get { return _bytesSent; } }
        public string ObjectSize { get { return _objectSize; } }
        public string TotalTime { get { return _totalTime; } }
        public string TurnAroundTime { get { return _turnAroundTime; } }
        public string Referrer { get { return _referrer; } } //
        public string UserAgent { get { return _userAgent; } }    //



        public static LogRecord Parse(string value)
        {

            int i = 0;
            LogRecord rt = new LogRecord();
            rt._bucketOwner = ReadUntil(value, ref i, ' ');
            rt._bucket = ReadUntil(value, ref i, ' ');
            rt._time = ReadUntil(value, ref i, ']');
            rt._remoteIP = ReadUntil(value, ref i, ' ');
            rt._requestor = ReadUntil(value, ref i, ' ');
            rt._requestID = ReadUntil(value, ref i, ' ');
            rt._operation = ReadUntil(value, ref i, ' ');
            rt._key = ReadUntil(value, ref i, ' ');
            rt._requestURI = ReadUntil(value, ref i, '"');
            rt._httpStatus = ReadUntil(value, ref i, ' ');
            rt._errorCode = ReadUntil(value, ref i, ' ');
            rt._bytesSent = ReadUntil(value, ref i, ' ');
            rt._objectSize = ReadUntil(value, ref i, ' ');
            rt._totalTime = ReadUntil(value, ref i, ' ');
            rt._turnAroundTime = ReadUntil(value, ref i, ' ');
            rt._referrer = ReadUntil(value, ref i, '"');
            rt._userAgent = ReadUntil(value, ref i, '"');
            return rt;
        }

        private static string ReadUntil(string value, ref int index, char end)
        {
            if (end != ' ')
            {
                index++;
            }
            int i = value.IndexOf(end, index);

            string rt = value.Substring(index, i - index);

            index = i + (end == ' ' ? 1 : 2);

            return rt;
        }

    }

}
